#include <server_check.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/signal.h>

#include <flache_debug.h>
#include <flache.h>

void *check_server_response( void *ptr );
int response;

int check_server(char *path)
{
    pthread_t thread;
    int ret;    
    int finished;

    finished = FALSE;
    response = -1;

    ret = pthread_create( &thread, NULL, check_server_response, (void*) path);

    if(ret == 0)     
    {
        int ii;
        for( ii = 0;ii < 10 && !finished;ii++)
        {
            sleep( DEFAULT_TIMER_SLEEP );
            finished = response != -1;
        }
    }

     pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);

     pthread_cancel(thread);
     pthread_exit( (void*) response);
}

void *check_server_response( void *ptr )
{
    char *path;
    path = (char *) ptr;

    struct stat st;
    if(stat(path,&st) != 0)
        response = -errno;
    else
        response = 0;

    return NULL;
}
